home *** CD-ROM | disk | FTP | other *** search
/ 3D GFX / 3D GFX.iso / amiutils / i_l / irit5 / triv_lib / triv_gen.c < prev    next >
C/C++ Source or Header  |  1995-12-30  |  25KB  |  528 lines

  1. /******************************************************************************
  2. * Triv_gen.c - General routines used by all modules of TRIV_lib.          *
  3. *******************************************************************************
  4. * Written by Gershon Elber, sep. 94.                          *
  5. ******************************************************************************/
  6.  
  7. #include <string.h>
  8. #include "triv_loc.h"
  9. #include "geomat3d.h"
  10. #include "miscattr.h"
  11.  
  12. /*****************************************************************************
  13. * DESCRIPTION:                                                               M
  14. * Allocates the memory required for a new trivariate.                        M
  15. *                                                                            *
  16. * PARAMETERS:                                                                M
  17. *   GType:      Type of geometry the curve should be - Bspline, Bezier etc.  M
  18. *   PType:      Type of control points (E2, P3, etc.).                       M
  19. *   ULength:    Number of control points in the U direction.                 M
  20. *   VLength:    Number of control points in the V direction.                 M
  21. *   WLength:    Number of control points in the W direction.                 M
  22. *                                                                            *
  23. * RETURN VALUE:                                                              M
  24. *   TrivTVStruct *:    An uninitialized freeform trivariate.                 M
  25. *                                                                            *
  26. * KEYWORDS:                                                                  M
  27. *   TrivTVNew, trivariates, allocation                                       M
  28. *****************************************************************************/
  29. TrivTVStruct *TrivTVNew(TrivGeomType GType,
  30.             CagdPointType PType,
  31.             int ULength,
  32.             int VLength,
  33.             int WLength)
  34. {
  35.     int i,
  36.     MaxAxis = CAGD_NUM_OF_PT_COORD(PType);
  37.     TrivTVStruct
  38.     *NewTV = (TrivTVStruct *) IritMalloc(sizeof(TrivTVStruct));
  39.  
  40.     NewTV -> GType = GType;
  41.     NewTV -> PType = PType;
  42.     NewTV -> ULength = ULength;
  43.     NewTV -> VLength = VLength;
  44.     NewTV -> WLength = WLength;
  45.     NewTV -> UVPlane = ULength * VLength;
  46.     NewTV -> UOrder = 0;
  47.     NewTV -> VOrder = 0;
  48.     NewTV -> WOrder = 0;
  49.     NewTV -> UKnotVector = NULL;
  50.     NewTV -> VKnotVector = NULL;
  51.     NewTV -> WKnotVector = NULL;
  52.     NewTV -> Pnext = NULL;
  53.     NewTV -> Attr = NULL;
  54.     NewTV -> Points[0] = NULL;                /* The rational element. */
  55.  
  56.     for (i = !CAGD_IS_RATIONAL_PT(PType); i <= MaxAxis; i++)
  57.     NewTV -> Points[i] = (CagdRType *) IritMalloc(sizeof(CagdRType) *
  58.                         ULength * VLength * WLength);
  59.  
  60.     for (i = MaxAxis + 1; i <= CAGD_MAX_PT_COORD; i++)
  61.     NewTV -> Points[i] = NULL;
  62.  
  63.     return NewTV;
  64. }
  65.  
  66. /*****************************************************************************
  67. * DESCRIPTION:                                                               M
  68. * Allocates the memory required for a new Bspline trivariate.                M
  69. *                                                                            *
  70. * PARAMETERS:                                                                M
  71. *   ULength:    Number of control points in the U direction.                 M
  72. *   VLength:    Number of control points in the V direction.                 M
  73. *   WLength:    Number of control points in the W direction.                 M
  74. *   UOrder:     Order of trivariate in the U direction.                 M
  75. *   VOrder:     Order of trivariate in the V direction.                 M
  76. *   WOrder:     Order of trivariate in the W direction.                 M
  77. *   PType:      Type of control points (E2, P3, etc.).                       M
  78. *                                                                            *
  79. * RETURN VALUE:                                                              M
  80. *   TrivTVStruct *:    An uninitialized freeform trivariate Bspline.         M
  81. *                                                                            *
  82. * KEYWORDS:                                                                  M
  83. *   TrivBspNew, trivariates, allocation                                      M
  84. *****************************************************************************/
  85. TrivTVStruct *TrivBspTVNew(int ULength, 
  86.                int VLength,
  87.                int WLength,
  88.                int UOrder,
  89.                int VOrder,
  90.                int WOrder,
  91.                CagdPointType PType)
  92. {
  93.     TrivTVStruct *TV;
  94.  
  95.     if (ULength < UOrder || VLength < VOrder || WLength < WOrder) {
  96.     TRIV_FATAL_ERROR(TRIV_ERR_WRONG_ORDER);
  97.     return NULL;
  98.     }
  99.  
  100.     TV = TrivTVNew(TRIV_TVBSPLINE_TYPE, PType, ULength, VLength, WLength);
  101.  
  102.     TV -> UKnotVector = (CagdRType *) IritMalloc(sizeof(CagdRType) *
  103.                                                            (UOrder + ULength));
  104.     TV -> VKnotVector = (CagdRType *) IritMalloc(sizeof(CagdRType) *
  105.                                                            (VOrder + VLength));
  106.     TV -> WKnotVector = (CagdRType *) IritMalloc(sizeof(CagdRType) *
  107.                                                            (WOrder + WLength));
  108.  
  109.     TV -> UOrder = UOrder;
  110.     TV -> VOrder = VOrder;
  111.     TV -> WOrder = WOrder;
  112.  
  113.     return TV;
  114. }
  115.  
  116. /*****************************************************************************
  117. * DESCRIPTION:                                                               M
  118. * Allocates the memory required for a new Bezier trivariate.                 M
  119. *                                                                            *
  120. * PARAMETERS:                                                                M
  121. *   ULength:    Number of control points in the U direction.                 M
  122. *   VLength:    Number of control points in the V direction.                 M
  123. *   WLength:    Number of control points in the W direction.                 M
  124. *   PType:      Type of control points (E2, P3, etc.).                       M
  125. *                                                                            *
  126. * RETURN VALUE:                                                              M
  127. *   TrivTVStruct *:    An uninitialized freeform trivariate Bezier.          M
  128. *                                                                            *
  129. * KEYWORDS:                                                                  M
  130. *   TrivBzrNew, trivariates, allocation                                      M
  131. *****************************************************************************/
  132. TrivTVStruct *TrivBzrTVNew(int ULength,
  133.                int VLength,
  134.                int WLength,
  135.                CagdPointType PType)
  136. {
  137.     TrivTVStruct
  138.     *TV = TrivTVNew(TRIV_TVBEZIER_TYPE, PType, ULength, VLength, WLength);
  139.  
  140.     TV -> UOrder = TV -> ULength = ULength;
  141.     TV -> VOrder = TV -> VLength = VLength;
  142.     TV -> WOrder = TV -> WLength = WLength;
  143.  
  144.     TV -> UKnotVector = TV -> VKnotVector = TV -> WKnotVector = NULL;
  145.  
  146.     return TV;
  147. }
  148.  
  149. /*****************************************************************************
  150. * DESCRIPTION:                                                               M
  151. * Allocates and duplicates all slots of a trivariate structure.             M
  152. *                                                                            *
  153. * PARAMETERS:                                                                M
  154. *   TV:        Trivariate to duplicate                                       M
  155. *                                                                            *
  156. * RETURN VALUE:                                                              M
  157. *   TrivTVStruct *:    Duplicated trivariate.                                M
  158. *                                                                            *
  159. * KEYWORDS:                                                                  M
  160. *   TrivTVCopy, trivariates                                                  M
  161. *****************************************************************************/
  162. TrivTVStruct *TrivTVCopy(TrivTVStruct *TV)
  163. {
  164.     int i, Len,
  165.     MaxAxis = CAGD_NUM_OF_PT_COORD(TV -> PType);
  166.     TrivTVStruct
  167.     *NewTV = (TrivTVStruct *) IritMalloc(sizeof(TrivTVStruct));
  168.  
  169.     NewTV -> GType = TV -> GType;
  170.     NewTV -> PType = TV -> PType;
  171.     NewTV -> ULength = TV -> ULength;
  172.     NewTV -> VLength = TV -> VLength;
  173.     NewTV -> WLength = TV -> WLength;
  174.     NewTV -> UVPlane = NewTV -> ULength * NewTV -> VLength;
  175.     NewTV -> UOrder = TV -> UOrder;
  176.     NewTV -> VOrder = TV -> VOrder;
  177.     NewTV -> WOrder = TV -> WOrder;
  178.     if (TV -> UKnotVector != NULL)
  179.     NewTV -> UKnotVector = BspKnotCopy(TV -> UKnotVector,
  180.                        TV -> ULength + TV -> UOrder);
  181.     else
  182.     NewTV -> UKnotVector = NULL;
  183.     if (TV -> VKnotVector != NULL)
  184.     NewTV -> VKnotVector = BspKnotCopy(TV -> VKnotVector,
  185.                        TV -> VLength + TV -> VOrder);
  186.     else
  187.     NewTV -> VKnotVector = NULL;
  188.     if (TV -> WKnotVector != NULL)
  189.     NewTV -> WKnotVector = BspKnotCopy(TV -> WKnotVector,
  190.                        TV -> WLength + TV -> WOrder);
  191.     else
  192.     NewTV -> WKnotVector = NULL;
  193.     NewTV -> Pnext = NULL;
  194.     NewTV -> Attr = NULL;
  195.     NewTV -> Points[0] = NULL;                /* The rational element. */
  196.  
  197.     Len = TV -> ULength * TV -> VLength * TV -> WLength;
  198.     for (i = !TRIV_IS_RATIONAL_TV(TV); i <= MaxAxis; i++) {
  199.     NewTV -> Points[i] = (CagdRType *) IritMalloc(sizeof(CagdRType) * Len);
  200.     CAGD_GEN_COPY(NewTV -> Points[i], TV -> Points[i],
  201.               sizeof(CagdRType) * Len);
  202.     }
  203.  
  204.     for (i = MaxAxis + 1; i <= CAGD_MAX_PT_COORD; i++)
  205.     NewTV -> Points[i] = NULL;
  206.  
  207.     return NewTV;
  208. }
  209.  
  210. /*****************************************************************************
  211. * DESCRIPTION:                                                               M
  212. * Duplicates a list of trivariate structures.                      M
  213. *                                                                            *
  214. * PARAMETERS:                                                                M
  215. *   TVList:    List of trivariates to duplicate.                             M
  216. *                                                                            *
  217. * RETURN VALUE:                                                              M
  218. *   TrivTVStruct *:  Duplicated list of trivariates.                         M
  219. *                                                                            *
  220. * KEYWORDS:                                                                  M
  221. *   TrivTVCopyList, trivariates                                              M
  222. *****************************************************************************/
  223. TrivTVStruct *TrivTVCopyList(TrivTVStruct *TVList)
  224. {
  225.     TrivTVStruct *TVTemp, *NewTVList;
  226.  
  227.     if (TVList == NULL)
  228.     return NULL;
  229.     TVTemp = NewTVList = TrivTVCopy(TVList);
  230.     TVList = TVList -> Pnext;
  231.     while (TVList) {
  232.     TVTemp -> Pnext = TrivTVCopy(TVList);
  233.     TVTemp = TVTemp -> Pnext;
  234.     TVList = TVList -> Pnext;
  235.     }
  236.     return NewTVList;
  237. }
  238.  
  239. /*****************************************************************************
  240. * DESCRIPTION:                                                               M
  241. * Deallocates and frees all slots of a trivariate structure.             M
  242. *                                                                            *
  243. * PARAMETERS:                                                                M
  244. *   TV:         Trivariate to free.                                          M
  245. *                                                                            *
  246. * RETURN VALUE:                                                              M
  247. *   void                                                                     M
  248. *                                                                            *
  249. * KEYWORDS:                                                                  M
  250. *   TrivTVFree, trivariates                                                  M
  251. *****************************************************************************/
  252. void TrivTVFree(TrivTVStruct *TV)
  253. {
  254.     int i, MaxAxis;
  255.  
  256.     if (TV == NULL)
  257.     return;
  258.  
  259.     MaxAxis = CAGD_NUM_OF_PT_COORD(TV -> PType);
  260.  
  261.     for (i = !TRIV_IS_RATIONAL_TV(TV); i <= MaxAxis; i++)
  262.     IritFree((VoidPtr) TV -> Points[i]);
  263.  
  264.     if (TV -> UKnotVector != NULL)
  265.     IritFree((VoidPtr) TV -> UKnotVector);
  266.     if (TV -> VKnotVector != NULL)
  267.     IritFree((VoidPtr) TV -> VKnotVector);
  268.     if (TV -> WKnotVector != NULL)
  269.     IritFree((VoidPtr) TV -> WKnotVector);
  270.  
  271.     AttrFreeAttributes(&TV -> Attr);
  272.     IritFree((VoidPtr) TV);
  273. }
  274.  
  275. /*****************************************************************************
  276. * DESCRIPTION:                                                               M
  277. * Deallocates and frees a list of trivariate structures.             M
  278. *                                                                            *
  279. * PARAMETERS:                                                                M
  280. *   TVList:    Trivariate list to free.                                      M
  281. *                                                                            *
  282. * RETURN VALUE:                                                              M
  283. *   void                                                                     M
  284. *                                                                            *
  285. * KEYWORDS:                                                                  M
  286. *   TrivTVFreeList, trivariates                                              M
  287. *****************************************************************************/
  288. void TrivTVFreeList(TrivTVStruct *TVList)
  289. {
  290.     TrivTVStruct *TVTemp;
  291.  
  292.     while (TVList) {
  293.     TVTemp = TVList -> Pnext;
  294.     TrivTVFree(TVList);
  295.     TVList = TVTemp;
  296.     }
  297. }
  298.  
  299. /*****************************************************************************
  300. * DESCRIPTION:                                                               M
  301. * Allocates the memory required for a new triangle.                          M
  302. *                                                                            *
  303. * PARAMETERS:                                                                M
  304. *   None                                                                     M
  305. *                                                                            *
  306. * RETURN VALUE:                                                              M
  307. *   TrivTriangleStruct *:  An uninitialized triangle.                        M
  308. *                                                                            *
  309. * KEYWORDS:                                                                  M
  310. *   TrivTriangleNew, allocation                                              M
  311. *****************************************************************************/
  312. TrivTriangleStruct *TrivTriangleNew(void)
  313. {
  314.     TrivTriangleStruct
  315.     *NewTri = (TrivTriangleStruct *)
  316.                 IritMalloc(sizeof(TrivTriangleStruct));
  317.  
  318.     NewTri -> Pnext = NULL;
  319.     NewTri -> Attr = NULL;
  320.  
  321.     return NewTri;    
  322. }
  323. /*****************************************************************************
  324. * DESCRIPTION:                                                               M
  325. * Allocates and duplicates all slots of a triangle structure.             M
  326. *                                                                            *
  327. * PARAMETERS:                                                                M
  328. *   TrivTriangleStruct *:  Triangle to duplicate.                            M
  329. *                                                                            *
  330. * RETURN VALUE:                                                              M
  331. *   TrivTriangleStruct *:  Duplicated triangle.                              M
  332. *                                                                            *
  333. * KEYWORDS:                                                                  M
  334. *   TrivTriangleCopy                                                         M
  335. *****************************************************************************/
  336. TrivTriangleStruct *TrivTriangleCopy(TrivTriangleStruct *Triangle)
  337. {
  338.     TrivTriangleStruct
  339.     *NewTri = (TrivTriangleStruct *)
  340.                 IritMalloc(sizeof(TrivTriangleStruct));
  341.  
  342.     GEN_COPY(NewTri, Triangle, sizeof(TrivTriangleStruct));
  343.  
  344.     NewTri -> Pnext = NULL;
  345.     NewTri -> Attr = NULL;
  346.  
  347.     return NewTri;
  348. }
  349.  
  350. /*****************************************************************************
  351. * DESCRIPTION:                                                               M
  352. * Duplicates a list of triangle structures.                      M
  353. *                                                                            *
  354. * PARAMETERS:                                                                M
  355. *   TriangleList:    List of triangle to duplicate.                 M
  356. *                                                                            *
  357. * RETURN VALUE:                                                              M
  358. *   TrivTriangleStruct *:  Duplicated list of triangle.                      M
  359. *                                                                            *
  360. * KEYWORDS:                                                                  M
  361. *   TrivTriangleCopyList                                                 M
  362. *****************************************************************************/
  363. TrivTriangleStruct *TrivTriangleCopyList(TrivTriangleStruct *TriangleList)
  364. {
  365.     TrivTriangleStruct *TriangleTemp, *NewTriangleList;
  366.  
  367.     if (TriangleList == NULL)
  368.     return NULL;
  369.     TriangleTemp = NewTriangleList = TrivTriangleCopy(TriangleList);
  370.     TriangleList = TriangleList -> Pnext;
  371.     while (TriangleList) {
  372.     TriangleTemp -> Pnext = TrivTriangleCopy(TriangleList);
  373.     TriangleTemp = TriangleTemp -> Pnext;
  374.     TriangleList = TriangleList -> Pnext;
  375.     }
  376.     return NewTriangleList;
  377. }
  378.  
  379. /*****************************************************************************
  380. * DESCRIPTION:                                                               M
  381. * Deallocates and frees all slots of a triangle structure.             M
  382. *                                                                            *
  383. * PARAMETERS:                                                                M
  384. *   Triangle:   Triangle to free.                                            M
  385. *                                                                            *
  386. * RETURN VALUE:                                                              M
  387. *   void                                                                     M
  388. *                                                                            *
  389. * KEYWORDS:                                                                  M
  390. *   TrivTriangleFree                                                         M
  391. *****************************************************************************/
  392. void TrivTriangleFree(TrivTriangleStruct *Triangle)
  393. {
  394.     if (Triangle)
  395.         return;
  396.  
  397.     AttrFreeAttributes(&Triangle -> Attr);
  398.     IritFree((VoidPtr) Triangle);
  399. }
  400.  
  401. /*****************************************************************************
  402. * DESCRIPTION:                                                               M
  403. * Deallocates and frees a list of triangle structures.                     M
  404. *                                                                            *
  405. * PARAMETERS:                                                                M
  406. *   TriangleList:    Triangle list to free.                                  M
  407. *                                                                            *
  408. * RETURN VALUE:                                                              M
  409. *   void                                                                     M
  410. *                                                                            *
  411. * KEYWORDS:                                                                  M
  412. *   TrivTriangleFreeList                                             M
  413. *****************************************************************************/
  414. void TrivTriangleFreeList(TrivTriangleStruct *TriangleList)
  415. {
  416.     TrivTriangleStruct *TriangleTemp;
  417.  
  418.     while (TriangleList) {
  419.     TriangleTemp = TriangleList -> Pnext;
  420.     TrivTriangleFree(TriangleList);
  421.     TriangleList = TriangleTemp;
  422.     }
  423. }
  424.  
  425. /*****************************************************************************
  426. * DESCRIPTION:                                                               M
  427. * Linearly transforms, in place, given TV as specified by Translate and      M
  428. * Scale.                                     M
  429. *                                                                            *
  430. * PARAMETERS:                                                                M
  431. *   TV:            Trivariate to transform.                                  M
  432. *   Translate:     Translation factor.                                       M
  433. *   Scale:         Scaling factor.                                           M
  434. *                                                                            *
  435. * RETURN VALUE:                                                              M
  436. *   void                                                                     M
  437. *                                                                            *
  438. * KEYWORDS:                                                                  M
  439. *   TrivTVTransform, trivariates                                             M
  440. *****************************************************************************/
  441. void TrivTVTransform(TrivTVStruct *TV, CagdRType *Translate, CagdRType Scale)
  442. {
  443.     switch (TV -> GType) {
  444.     case TRIV_TVBEZIER_TYPE:
  445.     case TRIV_TVBSPLINE_TYPE:
  446.         CagdTransform(TV -> Points,
  447.                   TV -> ULength * TV -> VLength * TV -> WLength,
  448.                       CAGD_NUM_OF_PT_COORD(TV -> PType),
  449.               !TRIV_IS_RATIONAL_TV(TV),
  450.                   Translate,
  451.                       Scale);
  452.         break;
  453.     default:
  454.         TRIV_FATAL_ERROR(TRIV_ERR_UNDEF_GEOM);
  455.         break;
  456.     }
  457. }
  458.  
  459. /*****************************************************************************
  460. * DESCRIPTION:                                                               M
  461. * Transforms, in place, the given TV as specified by homogeneous matrix Mat. M
  462. *                                                                            *
  463. * PARAMETERS:                                                                M
  464. *   TV:            Trivariate to transform.                                  M
  465. *   Mat:           Homogeneous transformation to apply to TV.                M
  466. *                                                                            *
  467. * RETURN VALUE:                                                              M
  468. *   void                                                                     M
  469. *                                                                            *
  470. * KEYWORDS:                                                                  M
  471. *   TrivTVMatTransform, trivariates                                          M
  472. *****************************************************************************/
  473. void TrivTVMatTransform(TrivTVStruct *TV, CagdMType Mat)
  474. {
  475.     switch (TV -> GType) {
  476.     case TRIV_TVBEZIER_TYPE:
  477.     case TRIV_TVBSPLINE_TYPE:
  478.         CagdMatTransform(TV -> Points,
  479.                      TV -> ULength * TV -> VLength * TV -> WLength,
  480.                          CAGD_NUM_OF_PT_COORD(TV -> PType),
  481.                  !TRIV_IS_RATIONAL_TV(TV),
  482.                      Mat);
  483.         break;
  484.     default:
  485.         TRIV_FATAL_ERROR(TRIV_ERR_UNDEF_GEOM);
  486.         break;
  487.     }
  488. }
  489.  
  490. /*****************************************************************************
  491. * DESCRIPTION:                                                               M
  492. * Converts a Bezier trivariate into a Bspline trivariate by adding two open  M
  493. * end uniform knot vectors to it.                         M
  494. *                                                                            *
  495. * PARAMETERS:                                                                M
  496. *   TV:        A Bezier trivariate to convert to a Bspline TV.               M
  497. *                                                                            *
  498. * RETURN VALUE:                                                              M
  499. *   TrivTVStruct *:  A Bspline trivariate representing the same geometry as  M
  500. *                    the given Bezier TV.                                    M
  501. *                                                                            *
  502. * KEYWORDS:                                                                  M
  503. *   TrivCnvrtBezier2BsplineTV, conversion, trivariate                        M
  504. *****************************************************************************/
  505. TrivTVStruct *TrivCnvrtBezier2BsplineTV(TrivTVStruct *TV)
  506. {
  507.     TrivTVStruct *BspTV;
  508.  
  509.     if (TV -> GType != TRIV_TVBEZIER_TYPE) {
  510.         TRIV_FATAL_ERROR(TRIV_ERR_UNDEF_GEOM);
  511.         return NULL;
  512.     }
  513.  
  514.     BspTV = TrivTVCopy(TV);
  515.  
  516.     BspTV -> UOrder = BspTV -> ULength;
  517.     BspTV -> VOrder = BspTV -> VLength;
  518.     BspTV -> WOrder = BspTV -> WLength;
  519.     BspTV -> UKnotVector = BspKnotUniformOpen(BspTV -> ULength,
  520.                           BspTV -> UOrder, NULL);
  521.     BspTV -> VKnotVector = BspKnotUniformOpen(BspTV -> VLength,
  522.                           BspTV -> VOrder, NULL);
  523.     BspTV -> WKnotVector = BspKnotUniformOpen(BspTV -> WLength,
  524.                           BspTV -> WOrder, NULL);
  525.     BspTV -> GType = TRIV_TVBSPLINE_TYPE;
  526.     return BspTV;
  527. }
  528.